package com.mz.mattress.admin.api.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.OrderUseRecordBonusQueryReq;
import com.mz.mattress.admin.api.resp.BonusMonthSum;
import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.OrderUseTimesBonusService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.OrderUseTimesBonus;
import com.mz.mattress.api.jwt.MerchantJwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(Constants.MERCHANT_PATH + "orderUseTimesBonus")
public class OrderUseTimesBonusController {

    @Autowired
    private OrderUseTimesBonusService orderUseTimesBonusService;

    @GetMapping("/listMonths")
    public Result<BonusMonthSum> listMonths(@RequestParam String months,
                                            @RequestParam Integer type,
                                            @RequestParam Integer typeId) {

        if (typeId.equals(Constants.TYPE_ID_NOT_FIND)) {
            return Result.ok();
        }

        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();

//        Integer type = null;
//        Integer typeId = null;
//        if (merchantId != null) {
//            type = Constants.ROLE_MERCHANT;
//            typeId = merchantId;
//        } else if (saleId != null) {
//            type = Constants.ROLE_SALE;
//            typeId = saleId;
//        }

        return orderUseTimesBonusService.listMonths(userJwt.getMemberMerId(), type, typeId, months);
    }

    @GetMapping("/listPage")
    public Result<IPage<OrderUseTimesBonus>> listPage(OrderUseRecordBonusQueryReq req) {

        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        req.setMemberId(userJwt.getMemberMerId());

        if (req.getMerchantId() != null) {
            req.setType(Constants.ROLE_MERCHANT);
            req.setTypeId(req.getMerchantId());
        } else if (req.getSaleId() != null) {
            req.setType(Constants.ROLE_SALE);
            req.setTypeId(req.getSaleId());
        } else if (req.getInvestorId() != null) {
            req.setType(Constants.ROLE_INVESTOR);
            req.setTypeId(req.getInvestorId());
        }

        return orderUseTimesBonusService.listPage(req);
    }

    @GetMapping(value = "/{id}")
    public Result<OrderUseTimesBonus> detail(@PathVariable Integer id) {
        return orderUseTimesBonusService.getById(id);
    }

}
